<?php $round = $sf_request->getParameter('round')?>
<?php $matchId = $match->getId()?>

<form action="<?php echo url_for('playerstat/save')?>" method="POST">

<input type="hidden" name="activeTeam" id="activeTeam" value=""/>
<input type="hidden" name="activePlayerNumber" id="activePlayerNumber" value=""/>

<input type="hidden" value="<?php echo $matchId?>" name="matchId"/>
<input type="hidden" value="<?php echo $round?>" name="round"/>
<style>
table tr td.player{cursor:pointer;}
table tr td {text-align:center;}
table tr td.active {background:yellow;}
table tr td.past {background:#eee!important;}
</style>

<?php $tfgm=0; $tfga=0; $tpt3m=0; $tpt3a=0; $tftm=0; $tfta=0; $toff=0; $treb=0; $tast=0; $ttov=0; $tstl=0; $tblk=0; $tpf=0; $tpts=0; ?>
<table width="100%">
  <tr>
     <td style="border:0;" width="25%" align="left">
        <h1><a style="font-size:20px;" href="#" onclick="$('#activeTeam').val('team1');$('#activePlayerNumber').val('');   $('table.main tr').removeClass('active'); $('table.main td').removeClass('active');  waiting = false; "><?php echo $team1.' - '.$match->getScore1()?></a></h1><br>
        <h1><a style="font-size:20px;" href="#" onclick="$('#activeTeam').val('team2');$('#activePlayerNumber').val('');   $('table.main tr').removeClass('active'); $('table.main td').removeClass('active');  waiting = false;  "><?php echo $team2.' - '.$match->getScore2()?></a></h1>
     </td>
     
     <td style="border:0;" width="50%">
        <table width="100%">
            <thead>
            <tr>
                <td><?php echo link_to('Round I', 'playerstat/index?round=1&matchId='.$matchId)?></td>
                <td><?php echo link_to('Round II', 'playerstat/index?round=2&matchId='.$matchId)?></td>
                <td><?php echo link_to('Round III', 'playerstat/index?round=3&matchId='.$matchId)?></td>
                <td><?php echo link_to('Round IV', 'playerstat/index?round=4&matchId='.$matchId)?></td>
                <td>Total</td>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td class="<?php echo ($round > 1) ? 'past ' : (($round == 1) ? 'active' : '')?>"><?php echo $match->getRound1Score1()?></td>
                <td class="<?php echo ($round > 2) ? 'past ' : (($round == 2) ? 'active' : '')?>"><?php echo $match->getRound2Score1()?></td>
                <td class="<?php echo ($round > 3) ? 'past ' : (($round == 3) ? 'active' : '')?>"><?php echo $match->getRound3Score1()?></td>
                <td class="<?php echo ($round > 4) ? 'past ' : (($round == 4) ? 'active' : '')?>"><?php echo $match->getRound4Score1()?></td>
                <td><?php echo $match->getScore1()?></td>
            </tr>
            <tr>
                <td class="<?php echo ($round > 1) ? 'past ' : (($round == 1) ? 'active' : '')?>"><?php echo $match->getRound1Score2()?></td>
                <td class="<?php echo ($round > 2) ? 'past ' : (($round == 2) ? 'active' : '')?>"><?php echo $match->getRound2Score2()?></td>
                <td class="<?php echo ($round > 3) ? 'past ' : (($round == 3) ? 'active' : '')?>"><?php echo $match->getRound3Score2()?></td>
                <td class="<?php echo ($round > 4) ? 'past ' : (($round == 4) ? 'active' : '')?>"><?php echo $match->getRound4Score2()?></td>
                <td><?php echo $match->getScore2()?></td>
            </tr>
            </tbody>
        </table>
     </td>
     <td style="border:0;" width="25%" align="right">
        <h1><a style="font-size:20px;" href="#" onclick="$('#activeTeam').val('team1');$('#activePlayerNumber').val('');   $('table.main tr').removeClass('active'); $('table.main td').removeClass('active');  waiting = false; "><?php echo $team1.' - '.$match->getScore1()?></a></h1><br>
        <h1><a style="font-size:20px;" href="#" onclick="$('#activeTeam').val('team2');$('#activePlayerNumber').val('');   $('table.main tr').removeClass('active'); $('table.main td').removeClass('active');  waiting = false;  "><?php echo $team2.' - '.$match->getScore2()?></a></h1>
     </td>
  </tr>
</table>

<table width="100%" class="main">
  <tr>
    <td nowrap valign="top" style="padding:0;border:0;">
        <table style="width:47%;float:left;">
          <thead>
            <tr>
              <th>#</th>
              <th>Player</th>
              <th>Fgm</th>
              <th>Fga</th>
              <th>Pt3m</th>
              <th>Pt3a</th>
              <th>Ftm</th>
              <th>Fta</th>
              <th>Off</th>
              <th>Reb</th>
              <th>Ast</th>
              <th>Tov</th>
              <th>Stl</th>
              <th>Blk</th>
              <th>Pf</th>
              <th>Pts</th>
            </tr>
          </thead>
          <tbody id="team1">
            <?php foreach ($players1 as $player): ?>             
              <?php $playerId = $player->getId(); ?>
              <?php $playerNumber = $player->getNumber(); ?>
              <?php $stat = isset($stats[$playerId]) ? $stats[$playerId] : new BbPlayerStat(); ?>
               <?php
              		$tfgm += $stat->getFgm();
              		$tfga += $stat->getFga();
              		$tpt3m += $stat->getPt3m();
              		$tpt3a += $stat->getPt3a();
              		$tftm += $stat->getFtm();
              		$tfta += $stat->getFta();
              		$toff += $stat->getOffreb();
              		$treb += $stat->getReb(); 
              		$tast += $stat->getAst(); 
              		$ttov += $stat->getTov(); 
              		$tstl += $stat->getStl(); 
              		$tblk += $stat->getBlk(); 
              		$tpf += $stat->getPf(); 
              		$tpts += $stat->getPts();	
              ?>
              <tr id="tr<?php echo sizeof($playerNumber) == 2 ? $playerNumber : '0'.$playerNumber?>">
                  <td><a name="player<?php echo $playerId?>"><?php echo $playerNumber?></a></td>
                  <td class="player"><?php echo $player ?></td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'fgm', {$playerId}, 1);"));?><br>
                    <input class="fgm" id="fgm<?php echo $playerId ?>" name="fgm<?php echo $playerId ?>" value="<?php echo $stat->getFgm() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'fgm', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'fga', {$playerId}, 1);"));?><br>
                    <input class="fga" id="fga<?php echo $playerId ?>" name="fga<?php echo $playerId ?>" value="<?php echo $stat->getFga() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'fga', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pt3m', {$playerId}, 1);"));?><br>
                    <input class="pt3m" id="pt3m<?php echo $playerId ?>" name="pt3m<?php echo $playerId ?>" value="<?php echo $stat->getPt3m() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pt3m', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pt3a', {$playerId}, 1);"));?><br>
                    <input class="pt3a" id="pt3a<?php echo $playerId ?>" name="pt3a<?php echo $playerId ?>" value="<?php echo $stat->getPt3a() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pt3a', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'ftm', {$playerId}, 1);"));?><br>
                    <input class="ftm" id="ftm<?php echo $playerId ?>" name="ftm<?php echo $playerId ?>" value="<?php echo $stat->getFtm() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'ftm', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'fta', {$playerId}, 1);"));?><br>
                    <input class="fta" id="fta<?php echo $playerId ?>" name="fta<?php echo $playerId ?>" value="<?php echo $stat->getFta() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'fta', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'off', {$playerId}, 1);"));?><br>
                    <input class="off" id="off<?php echo $playerId ?>" name="off<?php echo $playerId ?>" value="<?php echo $stat->getOffreb() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'off', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'reb', {$playerId}, 1);"));?><br>
                    <input class="reb" id="reb<?php echo $playerId ?>" name="reb<?php echo $playerId ?>" value="<?php echo $stat->getReb() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'reb', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'ast', {$playerId}, 1);"));?><br>
                    <input class="ast" id="ast<?php echo $playerId ?>" name="ast<?php echo $playerId ?>" value="<?php echo $stat->getAst() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'ast', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'tov', {$playerId}, 1);"));?><br>
                    <input class="tov" id="tov<?php echo $playerId ?>" name="tov<?php echo $playerId ?>" value="<?php echo $stat->getTov() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'tov', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'stl', {$playerId}, 1);"));?><br>
                    <input class="stl" id="stl<?php echo $playerId ?>" name="stl<?php echo $playerId ?>" value="<?php echo $stat->getStl() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'stl', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'blk', {$playerId}, 1);"));?><br>
                    <input class="blk" id="blk<?php echo $playerId ?>" name="blk<?php echo $playerId ?>" value="<?php echo $stat->getBlk() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'blk', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pf', {$playerId}, 1);"));?><br>
                    <input class="pf" id="pf<?php echo $playerId ?>" name="pf<?php echo $playerId ?>" value="<?php echo $stat->getPf() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pf', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pts', {$playerId}, 1);"));?><br>
                    <input class="pts" id="pts<?php echo $playerId ?>" name="pts<?php echo $playerId ?>" value="<?php echo $stat->getPts() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team1', 'pts', {$playerId}, -1);"));?>
                  </td>
              </tr>
            <?php endforeach; ?>
              <tr>
              	<td>
              		Totals 
              	</td>
              	<td>
              		 
              	</td>
              	<td>
              		<input id="total-fgm" name="total-fgm<?php echo $team1->getId() ?>" value="<?php echo $tfgm ?>" style="width:16px;margin:1px 0;"/><br>                    
              	</td>
              	<td>
              		<input id="total-fga" name="total-fga" value="<?php echo $tfga ?>" style="width:16px;margin:1px 0;"/><br>                    
              	</td>
              	<td>
              		<input id="total-pt3m" name="total-pt3m" value="<?php echo $tpt3m ?>" style="width:16px;margin:1px 0;"/><br>                                  
              	</td>
              	<td>
              		<input id="total-pt3a" name="total-pt3a" value="<?php echo $tpt3a ?> " style="width:16px;margin:1px 0;"/><br>                                  
              	</td>
              	<td>
              		<input id="total-ftm" name="total-ftm" value="<?php echo $tftm ?>" style="width:16px;margin:1px 0;"/><br>                                                	
              	</td>
              	<td>
              		<input id="total-fta" name="total-fta" value="<?php echo $tfta ?>" style="width:16px;margin:1px 0;"/><br>                                                	               
              	</td>
              	<td>
              		<input id="total-off" name="total-off" value="<?php echo $toff ?>" style="width:16px;margin:1px 0;"/><br>                                              	               
              	</td>
              	<td>
					         <input id="total-reb" name="total-reb" value="<?php echo $treb ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	
              	</td>
              	<td>
              		<input id="total-ast" name="total-ast" value="<?php echo $tast ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	 
              	</td>
              	<td>
					         <input id="total-tov" name="total-tov" value="<?php echo $ttov ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	
              	</td>
              	<td>
					         <input id="total-stl" name="total-stl" value="<?php echo $tstl ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		
              	</td>
              	<td>
              		<input id="total-blk" name="total-blk" value="<?php echo $tblk ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		 
              	</td>
              	<td>
              		<input id="total-pf" name="total-pf" value="<?php echo $tpf ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		 
              	</td>
              	<td>
              		<input id="total-pts" name="total-pts" value="<?php echo $tpts ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		 
              	</td>
              </tr>	
          </tbody>
        </table>
    </td>
    <td nowrap valign="top" style="padding:0;border:0;">
    <?php $tfgm=0; $tfga=0; $tpt3m=0; $tpt3a=0; $tftm=0; $tfta=0; $toff=0; $treb=0; $tast=0; $ttov=0; $tstl=0; $tblk=0; $tpf=0; $tpts=0; ?>
        <table style="width:48%;float:right;">
          <thead>
            <tr>
              <th>#</th>
              <th>Player</th>
              <th>Fgm</th>
              <th>Fga</th>
              <th>Pt3m</th>
              <th>Pt3a</th>
              <th>Ftm</th>
              <th>Fta</th>
              <th>Off</th>
              <th>Reb</th>
              <th>Ast</th>
              <th>To</th>
              <th>Stl</th>
              <th>Blk</th>
              <th>Pf</th>
              <th>Pts</th>
            </tr>
          </thead>
          <tbody id="team2">
            <?php foreach ($players2 as $player): ?>
              <?php $playerId = $player->getId(); ?>
              <?php $playerNumber = $player->getNumber(); ?>
              <?php $stat = isset($stats[$playerId]) ? $stats[$playerId] : new BbPlayerStat(); ?>
                 <?php 
              		$tfgm += $stat->getFgm(); 
              		$tfga += $stat->getFga(); 
              		$tpt3m += $stat->getPt3m(); 
              		$tpt3a += $stat->getPt3a(); 
              		$tftm += $stat->getFtm(); 
              		$tfta += $stat->getFta(); 
              		$toff += $stat->getOffreb(); 
              		$treb += $stat->getReb(); 
              		$tast += $stat->getAst(); 
              		$ttov += $stat->getTov(); 
              		$tstl += $stat->getStl(); 
              		$tblk += $stat->getBlk(); 
              		$tpf += $stat->getPf(); 
              		$tpts += $stat->getPts();	
              ?>
              <tr id="tr<?php echo sizeof($playerNumber) == 2 ? $playerNumber : '0'.$playerNumber?>">
                  <td><?php echo $playerNumber?></td>
                  <td class="player"><?php echo $player ?></td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'fgm', {$playerId}, 1);"));?><br>
                    <input class="fgm" id="fgm<?php echo $playerId ?>" name="fgm<?php echo $playerId ?>" value="<?php echo $stat->getFgm() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'fgm', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'fga', {$playerId}, 1);"));?><br>
                    <input class="fga" id="fga<?php echo $playerId ?>" name="fga<?php echo $playerId ?>" value="<?php echo $stat->getFga() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'fga', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pt3m', {$playerId}, 1);"));?><br>
                    <input class="pt3m" id="pt3m<?php echo $playerId ?>" name="pt3m<?php echo $playerId ?>" value="<?php echo $stat->getPt3m() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pt3m', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pt3a', {$playerId}, 1);"));?><br>
                    <input class="pt3a" id="pt3a<?php echo $playerId ?>" name="pt3a<?php echo $playerId ?>" value="<?php echo $stat->getPt3a() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pt3a', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'ftm', {$playerId}, 1);"));?><br>
                    <input class="ftm" id="ftm<?php echo $playerId ?>" name="ftm<?php echo $playerId ?>" value="<?php echo $stat->getFtm() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'ftm', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'fta', {$playerId}, 1);"));?><br>
                    <input class="fta" id="fta<?php echo $playerId ?>" name="fta<?php echo $playerId ?>" value="<?php echo $stat->getFta() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'fta', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'off', {$playerId}, 1);"));?><br>
                    <input class="off" id="off<?php echo $playerId ?>" name="off<?php echo $playerId ?>" value="<?php echo $stat->getOffreb() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'off', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'reb', {$playerId}, 1);"));?><br>
                    <input class="reb" id="reb<?php echo $playerId ?>" name="reb<?php echo $playerId ?>" value="<?php echo $stat->getReb() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'reb', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'ast', {$playerId}, 1);"));?><br>
                    <input class="ast" id="ast<?php echo $playerId ?>" name="ast<?php echo $playerId ?>" value="<?php echo $stat->getAst() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'ast', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'tov', {$playerId}, 1);"));?><br>
                    <input class="tov" id="tov<?php echo $playerId ?>" name="tov<?php echo $playerId ?>" value="<?php echo $stat->getTov() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'tov', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'stl', {$playerId}, 1);"));?><br>
                    <input class="stl" id="stl<?php echo $playerId ?>" name="stl<?php echo $playerId ?>" value="<?php echo $stat->getStl() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'stl', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'blk', {$playerId}, 1);"));?><br>
                    <input class="blk" id="blk<?php echo $playerId ?>" name="blk<?php echo $playerId ?>" value="<?php echo $stat->getBlk() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'blk', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pf', {$playerId}, 1);"));?><br>
                    <input class="pf" id="pf<?php echo $playerId ?>" name="pf<?php echo $playerId ?>" value="<?php echo $stat->getPf() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pf', {$playerId}, -1);"));?>
                  </td>
                  <td>
                    <?php echo image_tag('icons/arrow-up.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pts', {$playerId}, 1);"));?><br>
                    <input class="pts" id="pts<?php echo $playerId ?>" name="pts<?php echo $playerId ?>" value="<?php echo $stat->getPts() ?>" style="width:16px;margin:1px 0;"/><br>
                    <?php echo image_tag('icons/arrow-down.png', array("style"=>"cursor:pointer;margin:0;padding:0;border:0;", "class"=>"arrows", "onclick"=>"calc('team2', 'pts', {$playerId}, -1);"));?>
                  </td>
              </tr>
            <?php endforeach; ?>
            <tr>
              	<td>
              		Totals 
              	</td>
              	<td>
              		 
              	</td>
              	<td>
              		<input id="total-fgm" name="total-fgm2" value="<?php echo $tfgm ?>" style="width:16px;margin:1px 0;"/><br>                    
              	</td>
              	<td>
              		<input id="total-fga" name="total-fga2" value="<?php echo $tfga ?>" style="width:16px;margin:1px 0;"/><br>                    
              	</td>
              	<td>
              		<input id="total-pt3m" name="total-pt3m2" value="<?php echo $tpt3m ?>" style="width:16px;margin:1px 0;"/><br>                                  
              	</td>
              	<td>
              		<input id="total-pt3a" name="total-pt3a2" value="<?php echo $tpt3a ?> " style="width:16px;margin:1px 0;"/><br>                                  
              	</td>
              	<td>
              		<input id="total-ftm" name="total-ftm2" value="<?php echo $tftm ?>" style="width:16px;margin:1px 0;"/><br>                                                	
              	</td>
              	<td>
              		<input id="total-fta" name="total-fta2" value="<?php echo $tfta ?>" style="width:16px;margin:1px 0;"/><br>                                                	               
              	</td>
              	<td>
              		<input id="total-off" name="total-off2" value="<?php echo $toff ?>" style="width:16px;margin:1px 0;"/><br>                                              	               
              	</td>
              	<td>
					       <input id="total-reb" name="total-reb2" value="<?php echo $treb ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	
              	</td>
              	<td>
              		<input id="total-ast" name="total-ast2" value="<?php echo $tast ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	 
              	</td>
              	<td>
					       <input id="total-tov" name="total-tov2" value="<?php echo $ttov ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	
              	</td>
              	<td>
					       <input id="total-stl" name="total-stl2" value="<?php echo $tstl ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		
              	</td>
              	<td>
              		<input id="total-blk" name="total-blk2" value="<?php echo $tblk ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		 
              	</td>
              	<td>
              		<input id="total-pf" name="total-pf2" value="<?php echo $tpf ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		 
              	</td>
              	<td>
              		<input id="total-pts" name="total-pts2" value="<?php echo $tpts ?>" style="width:16px;margin:1px 0;"/><br>                                              	                             	              	               	              		 
              	</td>
              </tr>		
          </tbody>
        </table>

    </td>
  </tr>
</table>

<input type="submit" value="Хадгалах"/>
&nbsp;&nbsp;
<?php echo link_to('Тоглолтын жагсаалтруу буцах', 'match/index', array('class'=>'title'))?> 

</form>


<form id="formImport" action="<?php echo url_for('playerstat/import')?>" method="POST" enctype="multipart/form-data" class="right" style="margin:0 5px;">
  <input type="hidden" value="<?php echo $matchId?>" name="matchId"/>
  <input type="hidden" value="<?php echo $round?>" name="round"/>
  <table>
    <tr>
      <td valign="top" style="border:0;"><?php echo link_to(image_tag('icons/export_excel.png', array()).'<br>EXPORT', 'playerstat/export?matchId='.$matchId.'&round='.$round, array())?></td>
      <td valign="top" style="width:100px;border:0;">
        <a href="javascript:$('#formImport').submit();"><?php echo image_tag('icons/database_refresh.png', array())?><br>IMPORT</a>
        <br clear="all">
        <input type="file" name="importfile" id="importfile" size="5" style="width:10px;border:1px solid #C3D7DB;" class="left"/>
      </td>
    </tr>
  </table>
</form>

<script type="text/javascript">

// update related fields values
function calcRelateds(tbodyId, inputClass, inputId, value)
{
    variable = inputId.split(inputClass)
    playerId = variable[1];

    if(inputClass == "fgm")
    {
        $('#fga'+playerId).val(parseInt($('#fga'+playerId).val()) + parseInt(value));
        pts = 2; if(value == -1){pts = -2;}
        $('#pts'+playerId).val(parseInt($('#pts'+playerId).val()) + parseInt(pts));
        calcTotal(tbodyId, "fga");
        calcTotal(tbodyId, "pts");
    }
    else if(inputClass == "pt3m")
    {
        $('#pt3a'+playerId).val(parseInt($('#pt3a'+playerId).val()) + parseInt(value));
        $('#fgm'+playerId).val(parseInt($('#fgm'+playerId).val()) + parseInt(value));
        $('#fga'+playerId).val(parseInt($('#fga'+playerId).val()) + parseInt(value));
        pts = 3; if(value == -1){pts = -3;}
        $('#pts'+playerId).val(parseInt($('#pts'+playerId).val()) + parseInt(pts));
        calcTotal(tbodyId, "pt3a");
        calcTotal(tbodyId, "pts");
    }
    else if(inputClass == "ftm")
    {
        $('#fta'+playerId).val(parseInt($('#fta'+playerId).val()) + parseInt(value));
        $('#pts'+playerId).val(parseInt($('#pts'+playerId).val()) + parseInt(value));
        calcTotal(tbodyId, "fta");
        calcTotal(tbodyId, "pts");
    }
    else if(inputClass == "off")
    {
        $('#reb'+playerId).val(parseInt($('#reb'+playerId).val()) + parseInt(value));
        calcTotal(tbodyId, "reb");
    }

}


function calcTotal(tbodyId, inputClass)
{    
    sum = 0;
    inputs = "tbody#" + tbodyId + " ." + inputClass;
    $(inputs).each(function(index){
        sum = sum + parseInt($(this).val())
    });
    
    inputTotal = "tbody#" + tbodyId + " input#total-" + inputClass;
    $(inputTotal).val(sum);
}



function calc(tbodyId, inputClass, playerId, value)
{  
    number = parseInt($("#" + inputClass + playerId).val()) + parseInt(value);
    $("#" + inputClass + playerId).val(number);

    calcRelateds(tbodyId, inputClass, inputClass + playerId, value);
    
    calcTotal(tbodyId, inputClass);
}



$(document).ready(function()
{
  activeTR = "";
  activeTD = "";
  waiting = false;
  
  // select active row => tr.active
  $("table.main td.player").click(function()
  {
      $("table.main tr").removeClass('active');
      $("table.main td").removeClass('active');

      activeTeam = $(this).parent("tr:first").parent("tbody:first").attr('id');
      activePlayerNumber = $(this).parent("tr:first").attr('id');      
      
      activeTR = "table.main tbody#" + activeTeam + " tr#" + activePlayerNumber;
      activeTD = "table.main tbody#" + activeTeam + " tr#" + activePlayerNumber + " td";
      
      $(activeTR).addClass('active');      
      $(activeTD).addClass('active');

      waiting = true;
      
      $('#activeTeam').focusout();
      $('#activePlayerNumber').focusout();
  });


  // input key-up change total
  /*$("table.main input").keyup(function()
  {
      //value = parseInt($(this).val());
      //$(this).val(value);
      tbodyId = $(this).parent("td:first").parent("tr:first").parent("tbody:first").attr('id');
      inputClass = $(this).attr("class");
      //TODO: change relateds
      calcTotal(tbodyId, inputClass);
  });
  */
  
  
  // press key & increase|increase inputs
  $(document).bind('keypress', function(e) 
  {

    if(waiting == false)
    {              
          
          // bagiig shalgana
          activeTeam = $('#activeTeam').val();
          if(!activeTeam)
          {
              alert("Багаа сонгоно уу.");
              $('#activePlayerNumber').val("");
              return false;
          }
          
          activePlayerNumber = $('#activePlayerNumber').val();
          if(activePlayerNumber.length < 2)
          {
              newvalue = returnKeyValue(e.which);
              if(newvalue == "")
              {
                  alert("0-9 аль нэг товчийг дарна уу.");
                  return false;
              }
              $('#activePlayerNumber').val(activePlayerNumber + "" + newvalue);
          }
          
          activePlayerNumber = $('#activePlayerNumber').val();
          if(activePlayerNumber.length == 2)
          {
              // activate row          
              $("table.main tr").removeClass('active');
              $("table.main tr td").removeClass('active');
                            
              activeTR = "table.main tbody#" + activeTeam + " tr#tr" + activePlayerNumber;
              activeTD = "table.main tbody#" + activeTeam + " tr#tr" + activePlayerNumber + " td";
    
              $(activeTR).addClass('active');
              $(activeTD).addClass('active');
              
              waiting = true;
              $('#activeTeam').focusout();
              $('#activePlayerNumber').focusout();
          }
      }
      else
      {
            if(!activeTD)
            {
                alert("Тоглогчийн сонгоно уу.");
                return false;
            }

            // do incraese or decrease
            inputClass = "";
            inputId = "";
            
            switch(e.which)
            {
              case 50: //2
                fieldObj = $(activeTD + " input.fgm");
                $(activeTD + " input.fgm").val(parseInt(fieldObj.val()) + 1);
                inputClass = "fgm";
                inputId = fieldObj.attr("id");
                break;
              case 46: //.
                fieldObj = $(activeTD + " input.fga");
                $(activeTD + " input.fga").val(parseInt(fieldObj.val()) + 1);
                inputClass = "fga";
                break;
              case 51: //3
                fieldObj = $(activeTD + " input.pt3m");
                $(activeTD + " input.pt3m").val(parseInt(fieldObj.val()) + 1);
                inputClass = "pt3m";
                inputId = fieldObj.attr("id");
                break;
              case 13: //  enter
                fieldObj = $(activeTD + " input.pt3a");
                $(activeTD + " input.pt3a").val(parseInt(fieldObj.val()) + 1);
                inputClass = "pt3a";
                tmp = $('#activePlayerNumber').val();
                $('#activePlayerNumber').val(tmp);
                break;
              case 49: //1
                fieldObj = $(activeTD + " input.ftm");
                $(activeTD + " input.ftm").val(parseInt(fieldObj.val()) + 1);
                inputClass = "ftm";
                inputId = fieldObj.attr("id");
                break;
              case 48:  //0
                fieldObj = $(activeTD + " input.fta");
                $(activeTD + " input.fta").val(parseInt(fieldObj.val()) + 1);
                inputClass = "fta";
                break;
      
              case 53: //5
                fieldObj = $(activeTD + " input.off");
                $(activeTD + " input.off").val(parseInt(fieldObj.val()) + 1);
                inputClass = "off";
                inputId = fieldObj.attr("id");
                break;
              case 52: //4
                fieldObj = $(activeTD + " input.reb");
                $(activeTD + " input.reb").val(parseInt(fieldObj.val()) + 1);
                inputClass = "reb";
                break;
              case 54: //6
                fieldObj = $(activeTD + " input.ast");
                $(activeTD + " input.ast").val(parseInt(fieldObj.val()) + 1);
                inputClass = "ast";
                break;
              case 43: //+
                fieldObj = $(activeTD + " input.tov");
                $(activeTD + " input.tov").val(parseInt(fieldObj.val()) + 1);
                inputClass = "tov";
                break;
                
              case 55: //7
                fieldObj = $(activeTD + " input.stl");
                $(activeTD + " input.stl").val(parseInt(fieldObj.val()) + 1);
                inputClass = "stl";
                break;
              case 56: //8
                fieldObj = $(activeTD + " input.blk");
                $(activeTD + " input.blk").val(parseInt(fieldObj.val()) + 1);
                inputClass = "blk";
                break;
              case 57: //9
                fieldObj = $(activeTD + " input.pf");
                $(activeTD + " input.pf").val(parseInt(fieldObj.val()) + 1); 
                inputClass = "pf";
                break;
              case 118: //v
                fieldObj = $(activeTD + " input.pts");
                $(activeTD + " input.pts").val(parseInt(fieldObj.val()) + 1);
                inputClass = "pts";
                break;
            }
            
            
            switch(e.keyCode)
            {
              case 37:case 40: // left, down
                focusedInput = $('table.main input:focus');
                focusedInput.val(parseInt(focusedInput.val()) - 1);
                calcTotal(activeTeam, focusedInput.attr('class'));
                calcRelateds(activeTeam, focusedInput.attr('class'), focusedInput.attr('id'), -1);
                inputClass = "";
                break;
              case 38:case 39: // up, right
                focusedInput = $('table.main input:focus');
                focusedInput.val(parseInt(focusedInput.val()) + 1);
                calcTotal(activeTeam, focusedInput.attr('class'));
                calcRelateds(activeTeam, focusedInput.attr('class'), focusedInput.attr('id'), 1);
                inputClass = "";
                break;
            }
            
            if(inputClass != "")
            {
                calcTotal(activeTeam, inputClass);
            }
            if((inputClass != "") && (inputId != ""))
            {
                calcRelateds(activeTeam, inputClass, inputId, 1);
            }
        }
        
  });
  


});


function returnKeyValue(keycode)
{
  switch(keycode)
  {
      case 48: return "0";
      case 49: return "1";
      case 50: return "2";
      case 51: return "3";
      case 52: return "4";
      case 53: return "5";
      case 54: return "6";
      case 55: return "7";
      case 56: return "8";
      case 57: return "9";
      case 13: break;
      default: return "";
  }
}

</script>